<%--
  Created by IntelliJ IDEA.
  User: leixiaolong
  Date: 2018/9/13
  Time: 下午6:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>采购订单管理</title>
    <link rel="stylesheet" href="/static/css/rpt-condition.css" media="all" type="text/css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/layui/css/layui.css" media="all"
          type="text/css">
    <style>
        .layui-table-header {
            background-color: #1E9FFF;
        }

        .layui-table thead tr {
            background-color: #1E9FFF;
            color: black;
        }
    </style>
</head>
<body>
<div style="padding: 10px;">
    <div class="layui-form" style="border-color: black; border-style: solid; border-width: 1px;"
         lay-filter="select-condition">
        <div style="height: 20%; margin-top: 15px;">
            <div>
                <div>
                    <div class="layui-form-item layui-input-inline">
                        <label class="layui-form-label">交易起始日期：</label>
                        <input type="text" class="layui-input-inline dom-input" id="exch_start_date"
                               placeholder="yyyy-MM-dd">
                    </div>
                    <div class="layui-form-item layui-input-inline">
                        <label class="layui-form-label">交易结束日期：</label>
                        <input type="text" class="layui-input-inline dom-input" id="exch_end_date"
                               placeholder="yyyy-MM-dd">
                    </div>

                    <div class="layui-form-item layui-input-inline">
                        <label class="layui-form-label">收货起始日期：</label>
                        <input type="text" class="layui-input-inline dom-input" id="deli_start_date"
                               placeholder="yyyy-MM-dd">
                    </div>
                    <div class="layui-form-item layui-input-inline">
                        <label class="layui-form-label">收货结束日期：</label>
                        <input type="text" class="layui-input-inline dom-input" id="deli_end_date"
                               placeholder="yyyy-MM-dd">
                    </div>

                    <div class="layui-form-item layui-input-inline">
                        <label class="layui-form-label">供货商名称：</label>
                        <div class="layui-input-inline">
                            <select class="layui-form-select" id="supp_name">
                                <option value="" selected>全部</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item layui-input-inline">
                        <label class="layui-form-label">订单状态：</label>
                        <div class="layui-input-inline">
                            <select class="layui-form-select" id="order_stat">
                                <option value="" selected>全部</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-input-inline">
                        <label class="layui-form-label">审核状态：</label>
                        <div class="layui-input-inline">
                            <select class="layui-form-select" id="verfy_stat">
                                <option value="" selected>全部</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-input-inline">
                        <label class="layui-form-label">收货仓库：</label>
                        <div class="layui-input-inline">
                            <select class="layui-form-select" id="stor_name" lay-filter="stor_name">
                                <option value="" selected>全部</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div style="float: right; margin-right: 6%; margin-top: -50px;">
            <button class="layui-btn layui-btn-normal layui-btn-radius" lay-submit data-type="search">搜索</button>
            <button type="reset" class="layui-btn layui-btn-warm layui-btn-radius" style="margin-right: 20px;"
                    data-type="reset">重置
            </button>
        </div>
        <div style="clear:both"></div>
    </div>

    <div class="layui-btn-group" style="float: right; margin-right: 100px; margin-top: 5px;">
        <button class="layui-btn layui-btn-sm layui-btn-primary" lay-submit data-type="add">新增</button>
        <button class="layui-btn layui-btn-sm layui-btn-primary">导出</button>
    </div>

    <div style=" background-color: white; margin-top: 35px;">
        <table class="layui-hide" id="tab_purc_apply"></table>
    </div>
</div>
</body>
</html>

<script type="text/html" id="rowBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script language='javascript' src="${pageContext.request.contextPath}/plugins/layui/layui.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-select.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-date.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/utils/moment.js"></script>

<script>
    layui.use(["form", "element", "jquery", "layer", "laydate", "table"], function () {
        var form = layui.form, element = layui.element, layer = layui.layer, $ = layui.jquery;
        var laydate = layui.laydate, table = layui.table;

        var nowdate = moment().format('YYYY-MM-DD');
        renderDate('exch_start_date', nowdate);
        renderDate('exch_end_date', nowdate);
        renderDate('deli_start_date', nowdate);
        renderDate('deli_end_date', nowdate);

        initStorSelect("stor_name");
        initSuppInfoSelect("supp_name");
        initCodeTableSelect("order_stat", "/base/getPurcOrderStatList", null);
        initCodeTableSelect("verfy_stat", "/base/getVerfyStatList", null);

        var tableIns = table.render({
            id: 'tab_purc_apply',
            elem: '#tab_purc_apply',
            url: '/purc/getPurcOrderList',
            where: {
                makerId: '',
                exchStartDate: $('#exch_start_date').val(),
                exchEndDate: $('#exch_end_date').val(),
                deliStartDate: $('#deli_start_date').val(),
                deliEndDate: $('#deli_end_date').val(),
                suppCode: $('#supp_name').find('OPTION:selected').val(),
                stat: $('#order_stat').find('option:selected').val(),
                verfyStat: $('#verfy_stat').find('option:selected').val(),
                storCode: $('#stor_name').find('option:selected').val()
            },
            // cellMinWidth: 90,
            height: 'full-230',
            cols: [[
                {field: 'seqNo', title: '序号', type: 'numbers', align: 'center'},
                {field: 'docuMakerId', title: '制单人', align: 'center'},
                {
                    field: 'purcType', title: '采购类型', align: 'center',
                    templet: function (row) {
                        if (typeof(row.purcType) === 'undefined' || null == row.purcType) {
                            return '-';
                        }
                        switch (row.purcType) {
                            case 1:
                                return '现采';
                            case 2:
                                return '赊购'
                            default:
                                return row.purcType;
                        }
                    }
                },
                {field: 'exchDate', title: '交易日期', align: 'center'},
                {field: 'deliDate', title: '收货日期', align: 'center'},
                {field: 'storCode', title: '仓库代码', align: 'center', hide: true},
                {
                    field: 'storName', title: '仓库名称', align: 'center',
                    templet: function (row) {
                        if (typeof(row.storName) === 'undefined' || null == row.storName) {
                            return '-';
                        }
                        return row.storName;
                    }
                },
                {
                    field: 'digest', title: '摘要', align: 'center',
                    templet: function (row) {
                        if (typeof(row.digest) === 'undefined' || null == row.digest) {
                            return '-';
                        }
                        return row.digest;
                    }
                },
                {field: 'abbrName', title: '供货商简称', align: 'center'},
                {field: 'contName', title: '供货商联系人', align: 'center'},
                {field: 'contPhone', title: '联系电话', align: 'center'},
                {
                    field: 'stat', title: '状态', align: 'center',
                    templet: function (row) {
                        switch (row.stat) {
                            case 0:
                                return '待提交';
                            case 1:
                                return '待采购';
                            case 2:
                                return '待收货';
                            case 3:
                                return '已收货';
                            case 4:
                                return '部分收货';
                            case 5:
                                return '未收货';
                            default:
                                return row.purcType;
                        }
                    }
                },
                {
                    field: 'verfyStat', title: '审核状态', align: 'center',
                    templet: function (row) {
                        switch (row.verfyStat) {
                            case 0:
                                return '待提交审核';
                            case 1:
                                return '待审核';
                            case 2:
                                return '审核通过';
                            case 3:
                                return '审核未通过';
                            case 4:
                                return '反审核';
                            default:
                                return row.verfyStat;
                        }
                    }
                },
                {field: 'verfyErrMsg', title: '审核失败原因', align: 'center'},
                {
                    field: 'opertaor', title: '操作', align: 'center', templet: '#rowBar', fixed: 'right'
                }
            ]],
            done: function () {
                var myDate = new Date();
                layer.msg("数据渲染完成, date:" + myDate.getSeconds());
            },
            page: {
                limit: 20,
                limits: [10, 20, 50]
            }
        });

        /**
         * 监听表行事件
         */
        table.on('tool', function (obj) {
            //获得当前行数据
            var rowdata = obj.data;
            var layEvent = obj.event;//获得当前行事件
            var tr = obj.tr;//获得当前行tr的dom对象

            if (layEvent == 'edit') {//编辑
                debugger
                const index = layer.open({
                    type: 2,
                    title: ['入库单编辑', 'text-align: center; font-size: 20px;'],
                    area: ['94%', '90%'],
                    offset: 'auto',
                    content: '/purc/purc_order_edit',
                    success: function (layero, index) {
                        var iframe = window[layero.find('iframe')[0]['name']];
                        iframe.syncInit(rowdata);
                    }
                });
            } else if (layEvent == 'del') {//删除
                var wheredata = {orderId: rowdata.orderId}

                layer.confirm("是否删除本条记录", {icon: 3}, function (index) {
                    //联动调用后台删除入库单
                    $.ajax({
                        url: '/purc/delPurcOrder',
                        type: 'POST',
                        data: wheredata,
                        async: false,
                        success: function (result) {
                            layer.msg("删除入库单完成");
                            reloadTable();
                        },
                        error: function (error) {
                            layer.msg('删除入库单异常', error.toString());
                        }
                    });
                });
            } else {//其他暂不支持
                layer.msg("请等待开放功能");
            }

            //同步更新缓存对应的值
            // obj.update({username:'123', title: 'xxx'});
        });

        /**
         * 监听表行双击事件
         */
        table.on('rowDouble', function (obj) {
            var rowData = obj.data;

            var index = layer.open({
                type: 2,
                title: ['采购订单详情', 'text-align: center; font-size: 20px;'],
                area: ['94%', '90%'],
                offset: 'auto',
                content: '/purc/purc_order_receipts',
                success: function (layero, index) {
                    var iframe = window[layero.find('iframe')[0]['name']];
                    iframe.syncInit(rowData);
                }
            });
        });

        var actives = {
            search: function () {
                console.log($('#supp_name').find('option:selected').val());

                tableIns.reload({
                    where: {
                        makerId: '',
                        exchStartDate: $('#exch_start_date').val(),
                        exchEndDate: $('#exch_end_date').val(),
                        deliStartDate: $('#deli_start_date').val(),
                        deliEndDate: $('#deli_end_date').val(),
                        suppCode: $('#supp_name').find('OPTION:selected').val(),
                        stat: $('#order_stat').find('option:selected').val(),
                        verfyStat: $('#verfy_stat').find('option:selected').val(),
                        storCode: $('#stor_name').find('option:selected').val()
                    }
                });
            },
            add: function () {
                layer.open({
                    type: 2,
                    title: ['采购订单录入', 'text-align: center; font-size: 20px;'],
                    area: ['94%', '90%'],
                    offset: 'auto',
                    content: '/purc/purc_order_add'
                });
            },
            reset: function () {
                layer.msg("reset runing");
            }
        };

        /**
         * 监听按钮提交事件
         */
        form.on('submit', function (data) {
            actives[$(this).data("type")].call(this);
        });
    });

    function reloadTable() {
        layui.use(['table', 'jquery'], function () {
            var table = layui.table, $ = layui.jquery;
            debugger
            table.reload('tab_purc_apply', {
                where: {
                    makerId: '',
                    exchStartDate: $('#exch_start_date').val(),
                    exchEndDate: $('#exch_end_date').val(),
                    deliStartDate: $('#deli_start_date').val(),
                    deliEndDate: $('#deli_end_date').val(),
                    suppCode: $('#supp_name').find('OPTION:selected').val(),
                    stat: $('#order_stat').find('option:selected').val(),
                    verfyStat: $('#verfy_stat').find('option:selected').val(),
                    storCode: $('#stor_name').find('option:selected').val()
                }
            });
        })
    }
</script>
